package com.clear.honghefinance.admin.controller;


import com.clear.honghefinance.biz.modules.system.menu.converter.SysMenuConverter;
import com.clear.honghefinance.biz.modules.system.menu.domain.entity.SysMenu;
import com.clear.honghefinance.biz.modules.system.menu.domain.dto.CreateMenuDTO;
import com.clear.honghefinance.biz.modules.system.menu.domain.dto.DeleteMenuDTO;
import com.clear.honghefinance.biz.modules.system.menu.domain.dto.UpdateMenuDTO;
import com.clear.honghefinance.biz.modules.system.menu.domain.vo.GetMenuByIdVO;
import com.clear.honghefinance.biz.modules.system.menu.domain.vo.ListTreeMenuVO;
import com.clear.honghefinance.biz.modules.system.menu.domain.vo.ListTreeSelectMenuVO;
import com.clear.honghefinance.biz.modules.system.menu.service.ISysMenuService;
import com.clear.honghefinance.common.dto.entity.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "系统菜单管理")
@RestController
@RequestMapping("/sysMenu")
@Slf4j
public class SysMenuController {

    @Resource
    private ISysMenuService sysMenuService;


    /**
     * 新增菜单
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "创建菜单")
    @PostMapping(value = "/create")
    public ApiResponse<Boolean> create(@Validated @RequestBody CreateMenuDTO dto) {
        return ApiResponse.success(sysMenuService.create(dto));
    }

    /**
     * 修改菜单
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "修改菜单")
    @PostMapping(value = "/update")
    public ApiResponse<Boolean> update(@Validated @RequestBody UpdateMenuDTO dto) {
        return ApiResponse.success(sysMenuService.update(dto));
    }

    /**
     * 删除菜单 根据id删除
     *
     * @param dto req
     * @return
     */
    @ApiOperation(value = "删除菜单")
    @PostMapping(value = "/del")
    public ApiResponse<Boolean> del(@Validated @RequestBody DeleteMenuDTO dto) {
        return ApiResponse.success(sysMenuService.delete(dto));
    }

    /**
     * 获取菜单列表 树形展示
     *
     * @param name 菜单名称
     * @return
     */
    @ApiOperation(value = "树形菜单列表")
    @GetMapping(value = "/listTreeMenu")
    public ApiResponse<List<ListTreeMenuVO>> listTreeMenu(@RequestParam(required = false) String name) {
        return ApiResponse.success(sysMenuService.listTreeMenu(name));
    }


    @ApiOperation(value = "树形选择菜单列表")
    @GetMapping(value = "/listTreeSelectMenu")
    public ApiResponse<List<ListTreeSelectMenuVO>> listTreeSelectMenu() {
        return ApiResponse.success(sysMenuService.listTreeSelectMenu());
    }

    /**
     * 获取菜单明细
     *
     * @param id 菜单id
     * @return
     */
    @ApiOperation(value = "获取菜单明细")
    @GetMapping(value = "/getMenuById")
    public ApiResponse<GetMenuByIdVO> getMenuById(@Validated @RequestParam @NotNull(message = "菜单id不能为空") Integer id) {
        SysMenu sysMenu = sysMenuService.getMenuById(id);
        GetMenuByIdVO vo = SysMenuConverter.INSTANCE.convertSysMenuEntityToMenuByIdVO(sysMenu);
        return ApiResponse.success(vo);
    }

}
